package com.qijian.maindata.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.ExamineScheme;
import com.qijian.maindata.domain.LineProcess;
import com.qijian.maindata.domain.query.ExamineSchemeQuery;
import com.qijian.maindata.domain.query.LineProcessQuery;
import com.qijian.maindata.domain.vo.ExamineSchemeVo;
import com.qijian.maindata.service.IExamineSchemeService;
import com.qijian.maindata.service.ILineProcessService;
import com.qijian.tool.domain.Storage;
import com.qijian.tool.domain.query.StorageQuery;
import com.qijian.tool.service.IStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 检验方案Controller
 *
 * @author qijian
 * @date 2021-11-24
 */
@Api(tags = "质检管理：检验方案")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/scheme")
public class ExamineSchemeController extends BaseController {

    private final IExamineSchemeService examineSchemeService;

    private final ILineProcessService lineProcessService;

    private final IStorageService storageService;

    /**
     * 查询检验方案列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:scheme:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamineSchemeQuery query, IPage<ExamineSchemeVo> page) {
        return getDataTable(examineSchemeService.pageVo(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出检验方案列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:scheme:export')")
    @Log(title = "检验方案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(ExamineSchemeQuery query, HttpServletResponse response) {
        new ExcelUtil<>(ExamineScheme.class).exportExcel(response, examineSchemeService.list(WhereEntityTool.invoke(query)), "检验方案数据");
    }

    /**
     * 获取检验方案详细信息
     */
    @PreAuthorize("@ss.hasPermi('maindata:scheme:query')")
    @GetMapping(value = "/{schemeId}")
    public AjaxResult getInfo(@PathVariable("schemeId") Long schemeId) {
        ExamineScheme examineScheme = examineSchemeService.getById(schemeId);
        StorageQuery query = new StorageQuery();
        query.setClassId(schemeId);
        query.setClassTable("ExamineScheme");
        List<Storage> files = storageService.list(WhereEntityTool.invoke(query));
        examineScheme.setFiles(files);
        return AjaxResult.success(examineScheme);
    }

    /**
     * 新增检验方案
     */
    @PreAuthorize("@ss.hasPermi('maindata:scheme:add')")
    @Log(title = "检验方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamineSchemeVo vo) {
        return toAjax(examineSchemeService.saveData(vo));
    }

    /**
     * 修改检验方案
     */
    @PreAuthorize("@ss.hasPermi('maindata:scheme:edit')")
    @Log(title = "检验方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamineSchemeVo vo) {
        return toAjax(examineSchemeService.saveData(vo));
    }

    /**
     * 删除检验方案
     */
    @PreAuthorize("@ss.hasPermi('maindata:scheme:remove')")
    @Log(title = "检验方案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schemeIds}")
    public AjaxResult remove(@PathVariable List<Long> schemeIds) {
        LineProcessQuery processQuery = new LineProcessQuery();
        processQuery.setSchemeIds(schemeIds);
        List<LineProcess> list = lineProcessService.list(WhereEntityTool.invoke(processQuery));
        if (list.size() > 0) throw new ServiceException("E01:检验项目已被使用，无法删除！");
        return toAjax(examineSchemeService.removeByIds(schemeIds));
    }
}
